package com.aruio.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Description 测试全局过滤器
 * @Date 2024/8/22 13:55
 * @Created by 10574
 */
@Component
@Order(-1)
public class TestAuthGlobalFilter implements GlobalFilter {
    String TEST_ROLE = "admin";
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        final String role = exchange.getRequest().getQueryParams().getFirst("role");
        if (TEST_ROLE.equals(role)) {
            return chain.filter(exchange);
        }
        exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
        return exchange.getResponse().setComplete();
    }
}
